package com.jsoup.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jsoup.bean.Article;
import com.jsoup.bean.Comment;
import com.jsoup.service.ArticleService;
import com.jsoup.service.CommentService;
import com.jsoup.vo.CommentVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @Resource
    private CommentService commentService;

    /**
     * 博文详情
     * 浏览量+1
     * 加载用户评论数据
     *
     * @param articleId
     * @param model
     * @return
     */
    @RequestMapping("/getArticle")
    public String getArticle(@RequestParam(value = "articleId") Integer articleId, Model model) {
        Article a = new Article();
        a.setArticleId(articleId);
        Article article = articleService.getArticle(a);
        articleService.updateArticleViews(articleId);
        if (article != null) {
            model.addAttribute("article", article);
        }

        List<CommentVo> commentVoList = new ArrayList<>();
        List<Comment> list = commentService.selectComment(new Comment(articleId));

        for (Comment parent : list) {
            List<Comment> childrenList = new ArrayList<>();
            if (parent.getParentCommentId() == 0) {
                // 是父评论
                for (Comment children : list) {
                    if (children.getParentCommentId() != 0) {
                        // 是当前父评论的子评论
                        if (children.getParentCommentId() == parent.getCommentId()) {
                            childrenList.add(children);
                        }
                    }
                }
                commentVoList.add(new CommentVo(parent, childrenList));
            }
        }

        model.addAttribute("commentVoList", commentVoList);
        return "article-detail";
    }

    /**
     * 搜索博文
     *
     * @param pn
     * @param content
     * @param model
     * @return
     */
    @RequestMapping("/searchArticle")
    public String searchArticle(@RequestParam(value = "pn", defaultValue = "1") String pn,
                                @RequestParam(value = "content") String content,
                                Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        Article article = new Article();
        article.setArticleTitle(content);
        PageHelper.startPage(pageNo, 12);
        List<Article> articleList = articleService.searchArticle(article);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        model.addAttribute("pageInfo", pageInfo);
        return "article-search";
    }

    /**
     * 博文列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/articleList")
    public String articleList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 12);
        List<Article> articleList = articleService.selectArticle(new Article());
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        model.addAttribute("pageInfo", pageInfo);
        return "article-list";
    }

}
